package com.chendongyang.www.dao;//进行用户对停车的选择（停车或结束停车）

import com.chendongyang.www.po.parkingstation;
import com.mysql.cj.jdbc.Driver;

import java.sql.*;
import java.util.ArrayList;

public class stop {
    public ArrayList<parkingstation> select(String Id) throws SQLException {
        // 判断用户现在是否还在停车
        String  id=Id;
        //System.out.println(id);
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from stoping where 停车用户 =?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        //Statement statement=root.createStatement();
        preparedStatement.setString(1,id);
        ResultSet resultSet=preparedStatement.executeQuery();
        //System.out.println(resultSet.getString(1));
        //preparedStatement.setInt(1,id);
        //System.out.println(List.get(1));
        ArrayList<parkingstation> list=new ArrayList<>();
        while(resultSet.next()){
            parkingstation user=new parkingstation();
            user.setParkingS(resultSet.getString(1));
            user.setParkingB(resultSet.getString(2));
            list.add(user);
        }
        root.close();
        preparedStatement.close();
        resultSet.close();
        return list;
    }
    public ArrayList<parkingstation>  select1() throws SQLException {
        //查询可用于停车的停车位
        ArrayList<parkingstation> List=new ArrayList<>();
        DriverManager.registerDriver(new Driver());
        Connection root=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle","root","123456");
        String sql="select * from  stoping where 停车用户=0;";
        //PreparedStatement preparedStatement = root.prepareStatement(sql);
        Statement statement=root.createStatement();
        ResultSet resultSet=statement.executeQuery(sql);
        //System.out.println(resultSet.getString(1));
        while(resultSet.next()){
            parkingstation a=new parkingstation();
            a.setParkingS(resultSet.getString(1));
            a.setParkingB(resultSet.getString(2));
            List.add(a);
            //System.out.println(resultSet.getString(1));
            //System.out.println(resultSet.getString(2)+"0");
        }
        //System.out.println(List.get(1));
        // int i=List.size();
        root.close();
        statement.close();
        resultSet.close();
        return List;
    }
    public  int update(String station, String bit, String Id) throws SQLException {
        //客户停车位进行选择；
        DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="update stoping set 停车用户=? where 停车口 =? and 停车位=?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,Id);
        preparedStatement.setString(2,station);
        preparedStatement.setString(3,bit);
        int line =0;
        line = preparedStatement.executeUpdate();
        root.close();
        preparedStatement.close();
        return line;
    }
    public  int over(String id) throws SQLException {
        //客户结束停车
        DriverManager.registerDriver(new Driver());
        Connection root = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/lle", "root", "123456");
        String sql="update stoping set 停车用户=0 where 停车用户 =?;";
        PreparedStatement preparedStatement = root.prepareStatement(sql);
        preparedStatement.setString(1,id);
        int line =0;
        line = preparedStatement.executeUpdate();
        root.close();
        preparedStatement.close();
        return line;
    }
}
